1006F - Xor-Paths - CodeForces Solution


bitmasks brute force dp meet-in-the-middle *2100

Please click on ads to support us..

Python Code:

import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect
BUFSIZE = 4096
MOD1 = 10**9 + 7
MOD2 = 998244353

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def I():
    return input()

def II():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(input().split())

def LII():
    return list(map(int, input().split()))

def GMI():
    return map(lambda x: int(x) - 1, input().split())

def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))



n, m, k = MI()
h = []
for _ in range(n):
    h.append(LII())
d = defaultdict(int)
def p1(i, j, c):
    if i+j == (n+m-2) // 2:
        d[i, j, c] += 1
        return
    if i+1 < n:
        p1(i+1, j, c ^ h[i+1][j])
    if j+1 < m:
        p1(i, j+1, c ^ h[i][j+1])
p1(0, 0, h[0][0])
ans = 0
def p2(i, j, c):
    global ans
    if i+j == (n+m-2) // 2:
        ans += d[i, j, c ^ k]
        return
    if i-1 >= 0:
        p2(i-1, j, c ^ h[i][j])
    if j-1 >= 0:
        p2(i, j-1, c ^ h[i][j])
p2(n-1, m-1, 0)
print(ans)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#define iset indexed_set
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define ff first
#define ss second
#define mk make_pair
#define pb push_back
#define rep(i, start, end) for (int i = start; i < end; i++)
#define ld long double
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int inf = 1000000000000000000;
using ii = pair<int, int>;
const static int mx = 2e5 + 100;
int n, m, k;
int arr[25][25];
map<int, int> mp[21][21];
int val, val1;
int ans = 0;
void dfs(int x, int y, int cnt, int curr)
{
    if (cnt == 0)
    {
        mp[x][y][curr ^ arr[x][y]]++;
        return;
    }
    if (x + 1 < n)
    {
        dfs(x + 1, y, cnt - 1, curr ^ arr[x][y]);
    }
    if (y + 1 < m)
    {
        dfs(x, y + 1, cnt - 1, curr ^ arr[x][y]);
    }
}
void dfs1(int x, int y, int cnt, int curr)
{
    if (cnt == 0)
    {
        ans += mp[x][y][(curr ^ k)];
        return;
    }
    if (x - 1 >= 0)
    {
        dfs1(x - 1, y, cnt - 1, curr ^ arr[x][y]);
    }
    if (y - 1 >= 0)
    {
        dfs1(x, y - 1, cnt - 1, curr ^ arr[x][y]);
    }
}
void solve()
{
    cin >> n >> m >> k;
    rep(i, 0, n)
    {
        rep(j, 0, m)
        {
            cin >> arr[i][j];
        }
    }
    val = (n + m - 2) / 2;
    val1 = (n + m - 2) - val;
    dfs(0, 0, val, 0);
    dfs1(n - 1, m - 1, val1, 0);
    cout << ans << "\n";
}
// don't get stuck on a idea
// always write brute force if you cannot think of anything....remember you are anyways not going to
// do anything during the contest
// if constraints are small try to think about dp
// take a deep breathe and think. No hurry.
signed main()
{
    fast
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected